local CardPlayPanel = class("CardPlayPanel", BasePanel)

local PokerNumColor = {
    [PokerType.PokerType_heart] = Color(1, 0, 0, 1), --红桃
    [PokerType.PokerType_club] = Color(0, 0, 0, 1), --梅花
    [PokerType.PokerType_diamond] = Color(1, 0, 0, 1), --方块
    [PokerType.PokerType_spade] = Color(0, 0, 0, 1) --黑桃
}

function CardPlayPanel:ctor(goParent, res, args)
    CardPlayPanel.super.ctor(self, goParent, res, args)

    self.currSelPoker = 0
end

function CardPlayPanel:start()
    local go = self.go

    local goCardsTable = util.pick(go, "goCardsTable")

    local goCardSlot1 = util.pick(goCardsTable, "goCardSlot1")
    local goCardSlot2 = util.pick(goCardsTable, "goCardSlot2")
    local goCardSlot3 = util.pick(goCardsTable, "goCardSlot3")
    local goCardSlot4 = util.pick(goCardsTable, "goCardSlot4")

    local goCardSlots = {goCardSlot1, goCardSlot2, goCardSlot3, goCardSlot4}
    self.goCardSlots = goCardSlots

    local cardSprites = {
        [PokerType.PokerType_heart] = GameMgr.instance.resMgr:LoadSprite("Sprites/hearts"),
        [PokerType.PokerType_club] = GameMgr.instance.resMgr:LoadSprite("Sprites/clubs"),
        [PokerType.PokerType_diamond] = GameMgr.instance.resMgr:LoadSprite("Sprites/diamonds"),
        [PokerType.PokerType_spade] = GameMgr.instance.resMgr:LoadSprite("Sprites/spades")
    }
    print(cardSprites[PokerType.PokerType_spade])
    self.cardSprites = cardSprites

    local prefab = GameMgr.instance.resMgr:LoadAsset("Prefabs/PokerNormalTemplate")

    local pokers = {}
    for i, goCardSlot in ipairs(goCardSlots) do
        local goPoker = util.newObject(prefab, goCardSlot.transform)
        local pokerItem = {goPoker = goPoker, index = i}
        self:init_poker(pokerItem)
        pokers[i] = pokerItem
    end
    self.pokers = pokers

    local prefab = GameMgr.instance.resMgr:LoadAsset("Prefabs/OperationView")
    local goOperationView = util.newObject(prefab, go.transform)
    goOperationView:SetActive(false)
    local btnAdd = util.pickC(goOperationView,"btnAdd","Button")
    btnAdd.onClick:AddListener(function()
    end)
    local btnDel = util.pickC(goOperationView,"btnDel","Button")
    btnDel.onClick:AddListener(function()
    end)
    local btnMul = util.pickC(goOperationView,"btnMul","Button")
    btnMul.onClick:AddListener(function()
    end)
    local btnDiv = util.pickC(goOperationView,"btnDiv","Button")
    btnDiv.onClick:AddListener(function()
    end)
    self.goOperationView = goOperationView
    
    local game = {
        pokers = {
            {type = PokerType.PokerType_diamond, number = 1},
            {type = PokerType.PokerType_club, number = 2},
            {type = PokerType.PokerType_heart, number = 3},
            {type = PokerType.PokerType_spade, number = 4}
        }
    }
    self:set_game(game)
end

function CardPlayPanel:init_poker(pokerItem)
    local goPoker = pokerItem.goPoker

    local btnPoker = util.pickC(goPoker, "", "Button")
    btnPoker.onClick:AddListener(
        function()
            self:on_click_poker(pokerItem.index)
        end
    )
    pokerItem.btnPoker = btnPoker

    local imgType = util.pickC(goPoker, "imgType", "Image")
    pokerItem.imgType = imgType

    local lbNumber = util.pickC(goPoker, "lbNumber", "Text")
    pokerItem.lbNumber = lbNumber

    local outlineSelect = util.pickC(goPoker, "imgBkg", "Outline")
    outlineSelect.enabled = false
    pokerItem.outlineSelect = outlineSelect
end

function CardPlayPanel:set_poker(pokerItem, type, number)
    local cardSprites = self.cardSprites
    local imgType = pokerItem.imgType
    local lbNumber = pokerItem.lbNumber

    imgType.sprite = cardSprites[type]

    lbNumber.text = PokerNumStr[number] or tostring(number)
    lbNumber.color = PokerNumColor[type]
end

function CardPlayPanel:on_click_poker(index)
    local pokers = self.pokers
    local goOperationView = self.goOperationView
    local currSelPoker = self.currSelPoker
    if currSelPoker == 0 then
        self.currSelPoker = index
        pokers[index].outlineSelect.enabled = true
    else
        if index == currSelPoker then
            self.currSelPoker = 0
            pokers[index].outlineSelect.enabled = false
            goOperationView:SetActive(false)
        else
            pokers[index].outlineSelect.enabled = true
            goOperationView:SetActive(true)
        end
    end
end

function CardPlayPanel:set_game(game)
    local pokers = self.pokers
    for i, pokerItem in ipairs(pokers) do
        self:set_poker(pokerItem, game.pokers[i].type, game.pokers[i].number)
    end
end

return CardPlayPanel
